import { EVENT_NAME_ENUM } from "../base/EnumIndex";
import LevelBase from "../LevelBase";
import { EventManager } from "../manager/EventManager";
import LevelPre_15 from "./Game_15/LevelPre_15";

const { ccclass, property } = cc._decorator;
var WinType = cc.Enum({
    DaYu: 1,
    XiaoYu: 2,
    DengYu: 3,
});

@ccclass
export default class MoneyItem extends cc.Component {
    @property(cc.Float)  //初始钱数
    initNum: number = 0;
    @property(cc.Float)   //胜利钱数
    winNum: number = 200;
    @property({ type: cc.Enum(WinType), displayName: "胜利条件" })  //胜利条件
    winType: number = WinType.DaYu;
    /** 判断胜利后 执行的Tween */
    @property(cc.String)
    runWinTween: string = "";
    /** 判断失败后 执行的Tween */
    @property(cc.String)
    runOverTween: string = "";
    onLoad() {
        EventManager.ins.on(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finish, this);
    }
    onDestroy() {
        EventManager.ins.off(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finish, this);
    }
    start() {

        this.updateMoney(0);
    }
    finish(finishStr: string) {
        if (typeof finishStr != "string") {
            return;
        }
        if (finishStr.includes("updateMoeny_")) {
            let str = finishStr.split("updateMoeny_")[1];
            this.updateMoney(parseInt(str));
        }
        if (finishStr == "updateMoneyIsWin") {
            this.runIsWinTween();
        }
        if (finishStr == "updateMoney&StateIsWin") { //目标数值跟添加的状态都满足才算过关
            this.runIsWinTween2();
        }
    }
    /** 刷新报价 */
    updateMoney(num: number = 0) {
        this.initNum += num;
        EventManager.ins.emit(EVENT_NAME_ENUM.UPDATE_GOAL, this.initNum)
    };
    /** 执行是否胜利Tween */
    runIsWinTween() {
        if (this.getIsWin() && this.runWinTween != "") {
            let levelBase = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelBase);
            levelBase.runNodeTween(levelBase.node, levelBase.getInterTwForName(this.runWinTween).interTween);
        } else if (!this.getIsWin() && this.runOverTween != "") {
            let levelBase = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelBase);
            levelBase.runNodeTween(levelBase.node, levelBase.getInterTwForName(this.runOverTween).interTween);
        }
    };
    /** 执行是否胜利Tween */
    runIsWinTween2() {
        if (this.getIsWin() && this.runWinTween != "") {
            let levelBase = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelBase);
            levelBase.runNodeTween(levelBase.node, levelBase.getInterTwForName(this.runWinTween).interTween);
        } else if (!this.getIsWin() && this.runOverTween != "") {
            if(this.getIsWinState()){
                let levelBase = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelBase);
                levelBase.runNodeTween(levelBase.node, levelBase.getInterTwForName(this.runOverTween).interTween);
            }else{

            }
        }
    };
    /** 是否过关 */
    getIsWin() {
        if (this.winType == WinType.DengYu) {
            if (this.initNum == this.winNum) { return true; }
            return false;
        }
        if (this.winType == WinType.XiaoYu && this.initNum <= this.winNum) {
            return true;
        } else if (this.winType == WinType.DaYu && this.initNum >= this.winNum) {
            return true;
        }
        return false;
    };

    /** 是否状态满足过关 */
    getIsWinState() {
        let pre15 = cc.find("Canvas").getComponentInChildren(LevelPre_15);

        if (pre15.interJson.winStates.length != pre15.finishStateArr.length || pre15.finishStateArr.length <= 0) { return false; }
        for (let i = 0; i < pre15.finishStateArr.length; i++) {
            if (pre15.interJson.winStates.indexOf(pre15.finishStateArr[i]) < 0) {
                return false;
            }
        }
        return true;
    };
    // update (dt) {}
}
